package com.xy.sql.xml.node;

import com.xy.sql.xml.core.XySQLExePara;
import com.xy.sql.xml.core.XySQLTaskContext;
import com.xy.sql.xml.itf.XySQLRegister;
import com.xy.sql.xml.kit.XySQLScriptKit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SQL节点
 */
public abstract class XySQLSuperNode {

    //子节点类型
    private List<XySQLSuperNode> childList;
    //属性列表
    private Map<String, String> propertyMap = new HashMap<>();
    //属性内容
    private String txt;
    //主键
    private String id;
    //开始
    private String beginTag;
    //结束
    private String endTag;


    /**
     * 取得当前节点得ID
     *
     * @return
     */
    public String getId() {
        if (id == null) {
            id = propertyMap.get("id");
            if (id == null) {
                id = "";
            }
        }
        return id;
    }

    /**
     * 取得指定SQL片段
     *
     * @param inNextId
     * @return
     */
    public XySQLSuperNode getSqlSegmentById(String inNextId) {
        if (childList == null || inNextId == null) {
            return null;
        }
        for (XySQLSuperNode node : childList) {
            if (inNextId.equalsIgnoreCase(node.getId())) {
                return node;
            }
        }
        return null;
    }

    /**
     * 解析
     *
     * @return
     * @throws Exception
     */
    public void parse() throws Exception {
        propertyMap.putAll(XySQLScriptKit.prop(this.getTag(), getTxt(), " "));
        if (!propertyMap.containsKey("id")) {
            String name = propertyMap.get("name");
            if (name != null) {
                propertyMap.put("id", name);
            }
        }
    }

    /**
     * 构造SQL，绑定参数
     *
     * @param task
     * @return
     * @throws Exception
     */
    public List<XySQLExePara> boundSql(XySQLTaskContext task) throws Exception {
        long start = System.currentTimeMillis();
        List<XySQLExePara> resList = new ArrayList<>();
        if (childList != null) {
            for (XySQLSuperNode node : childList) {
                resList.addAll(node.boundSql(task));
            }
        }
        XySQLRegister.getLogger().debug(this.getTag() + " boundSql,耗时：" + (System.currentTimeMillis() - start));
        return resList;
    }

    /**
     * 标签名称
     *
     * @return
     */
    public abstract String getTag();


    /**
     * 标签名称
     *
     * @return
     */
    public String getBeginTag() {
        if (beginTag == null) {
            beginTag = XySQLScriptKit.START_SIGN + this.getTag();
        }
        return beginTag;
    }

    /**
     * 标签名称
     *
     * @return
     */
    public String getEndTag() {
        if (endTag == null) {
            endTag = XySQLScriptKit.START_SIGN + "/" + this.getTag() + XySQLScriptKit.END_SIGN;
        }
        return endTag;
    }

    /**
     * 克隆自己
     *
     * @return
     */
    public abstract XySQLSuperNode clone();

    /**
     * 得到所有子对象
     *
     * @return
     */
    public List<XySQLSuperNode> getChildList() {
        return childList;
    }

    /**
     * 设定子对象
     *
     * @param childList
     */
    public void setChildList(List<XySQLSuperNode> childList) {
        this.childList = childList;
    }


    /**
     * 得到所有属性
     *
     * @return
     */
    public Map<String, String> getPropertyMap() {
        return propertyMap;
    }


    /**
     * 取得内容
     *
     * @return
     */
    public String getTxt() {
        return txt;
    }

    /**
     * 设定内容
     *
     * @param txt
     */
    public void setTxt(String txt) {
        this.txt = txt;
    }
}
